(function () {
    $.fn.extend({
        initOptions: function () {
            var options = {
                url: '',
                confirm: '',
                loading: true,
                callback: function () {}
            };
            return options;
        },
        ajaxPost: function (new_options) {
            var $this = this;
            var $btn = $(this);

            //合并参数
            var options = $.extend($this.initOptions(), new_options);

            if (options.confirm !== '') {
                $this.doConfirm(options.confirm, function () {
                    $this.doPost($this, $btn, options);
                });
            } else {
                $this.doPost($this, $btn, options);
            }


        },
        //post方法
        doPost: function ($this, $btn, options) {
            var data = {};
            if ($btn.parents('.ks-form').length > 0) {
                data = $btn.parents('.ks-form').find('*').serializeArray();
            } else {
                data = $('body *').serializeArray();
            }
            $.ajax({
                type: "get",
                url: options.url,
                data: data,
                dataType: "json",
                timeout: 30000,
                cache: false,
                beforeSend: function () {
                    if (options.loading)
                    {
                        $this.addLoading();
                    }
                    $btn.attr('disabled', true);
                },
                success: function (json) {
                    options.callback(json);//成功回调函数
                    if (typeof (json.msg) === 'string') {
                        $this.doAlert(json);
                    }
                    if (json.flag && typeof (json.msg) !== 'string' && typeof (json.url) === 'string') {
                        window.location.href = json.url;
                    }
                },
                complete: function () {
                    $this.removeLoading();
                    $btn.removeAttr('disabled');
                },
                error: function (e) {
                    console.log(e);
                }
            });
        },
        //添加加载层效果
        addLoading: function () {
            if ($("#ks-loading").length < 1) {
                var html_loading = '<div id="ks-loading"><div class="shade"></div><div class="spinner"><div class="bounce1"></div><div class="bounce2"></div><div class="bounce3"></div></div></div>';
                $('body').append(html_loading);
            } else {
                $("#ks-loading").show();
            }
        },
        //移除加载层效果
        removeLoading: function () {
            $("#ks-loading").remove();
        },
        //确认提交框
        doConfirm: function (confirm_text, callback) {
            this.removeDialog();
            var html_dialog = '<div id="ks-dialog" class="a-bouncein"><div class="shade"></div><div class="dialog"><div class="content">' + confirm_text + '</div><div class="foot"><button type="button" class="ks-btn ks-btn-cancel">取消</button><button type="button" class="ks-btn ks-btn-submit">确定</button></div></div></div>';
            $('body').append(html_dialog);

            $this = this;
            $("#ks-dialog .shade").bind('click', function () {
                $this.removeDialog();
            });
            $("#ks-dialog .ks-btn-cancel").bind('click', function () {
                $this.removeDialog();
            });
            $("#ks-dialog .ks-btn-submit").bind('click', function () {
                $(this).attr('disabled', true);
                if (typeof (callback) === 'function') {
                    callback();
                }

                $this.removeDialog();
            });
        },
        doAlert: function (json) {
            var style = json.flag ? '' : ' ks-btn-red';
            var btn_text = typeof (json.url) === 'string' ? '继续' : '确定';
            var goto_text = typeof (json.url) === 'string' ? '<span><b id="ks-wait">5</b>秒后页面跳转</span>' : '';

            this.removeDialog();
            var html_dialog = '<div id="ks-dialog" class="a-bouncein"><div class="shade"></div><div class="dialog"><div class="content">' + json.msg + '</div><div class="foot">' + goto_text + '<button type="button" class="ks-btn ks-btn-submit' + style + '">' + btn_text + '</button></div></div></div>';
            $('body').append(html_dialog);

            $this = this;
            if (typeof (json.url) === 'string') {
                var wait = window.document.getElementById("ks-wait");
                ks_alert_interval = setInterval(function () {
                    var time = --wait.innerHTML;
                    if (time <= 0) {
                        // hideAlert(url);
                        clearInterval(ks_alert_interval);
                        window.location.href = json.url;
                    }
                }, 1000);
                $("#ks-dialog .ks-btn-submit").bind('click', function () {
                    $(this).attr('disabled', true);
                    window.location.href = json.url;
                    $this.removeDialog();
                });
            } else {
                $("#ks-dialog .shade").bind('click', function () {
                    $this.removeDialog();
                });
                $("#ks-dialog .ks-btn-submit").bind('click', function () {
                    $(this).attr('disabled', true);
                    $this.removeDialog();
                });
            }



        }, //移除
        removeDialog: function () {
            $("#ks-dialog").remove();
        },
        //判断是否手机
//        checkMobile: function () {
//            var userAgentInfo = navigator.userAgent;
//            var Agents = ["Android", "iPhone",
//                "SymbianOS", "Windows Phone",
//                "iPad", "iPod"];
//            var flag = false;
//            for (var v = 0; v < Agents.length; v++) {
//                if (userAgentInfo.indexOf(Agents[v]) > 0) {
//                    flag = true;
//                    break;
//                }
//            }
//            return flag;
//        }
        resetForm: function () {
            var $this = this;
            $this.doConfirm("确认重置表单？", function () {
                if ($this.parents('.ks-form').length > 0) {
                    $this.parents('.ks-form')[0].reset();
                } else {
                    if ($this.parents('form').length > 0) {
                        $this.parents('form')[0].reset();
                    } else {
                        $('body')[0].reset();
                    }
                }

            });
        }

    });
})();